package com.zbkj.service.service.mszs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.model.mszs.MszsYwRecord;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.mszs.MszsYwRecordRequest;
import com.zbkj.common.response.mszs.MszsYwRecordResponse;
import com.zbkj.service.dao.mszs.MszsYwRecordDAO;
import com.zbkj.service.service.mszs.MszsYwRecordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 民宿助手-业务记录-servers
 * JIANTAO
 */
@Service
public class MszsYwRecordServiceImpl extends ServiceImpl<MszsYwRecordDAO, MszsYwRecord> implements MszsYwRecordService {

    @Autowired
    private MszsYwRecordDAO recordDAO;

    @Override
    public boolean insert(MszsYwRecordRequest request) {

        MszsYwRecord record = new MszsYwRecord();
        BeanUtils.copyProperties(request, record);
        boolean f = this.save(record);
        return f;
    }

    @Override
    public boolean delete(Integer id) {
        return this.removeById(id);
    }

    @Override
    public MszsYwRecordResponse load(Integer id) {

        MszsYwRecord r = this.getById(id);

        MszsYwRecordResponse response = new MszsYwRecordResponse();
        BeanUtils.copyProperties(r, response);
        return response;
    }

    @Override
    public List<MszsYwRecordResponse> pageList(String title, PageParamRequest pageParamRequest) {

        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        LambdaQueryWrapper<MszsYwRecord> rapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(title)){
            rapper.like(MszsYwRecord::getTitle,title);
        }
        rapper.orderByDesc(MszsYwRecord::getCreateTime);
        List<MszsYwRecord> records = recordDAO.selectList(rapper);

        List<MszsYwRecordResponse> responseList = records.stream().map(e -> {
            MszsYwRecordResponse response = new MszsYwRecordResponse();
            BeanUtils.copyProperties(e, response);
            return response;
        }).collect(Collectors.toList());
        return responseList;
    }
}
